package com.marui.ch3.answer;

import java.util.concurrent.atomic.AtomicInteger;

public class TestAt implements Runnable {
    private static AtomicInteger atomicI = new AtomicInteger(0);

    @Override
    public void run() {
        for (;;) {
            int i = atomicI.get();
            boolean suc = atomicI.compareAndSet(i, ++i);
            if (suc) {
                break;
            }
        }
       atomicI.getAndIncrement();
    }
    public int getCount() {
        return atomicI.get();
    }


    public static void main(String[] args) throws InterruptedException {
        TestAt a=null;
        Thread t=null;
        for (int i = 0; i < 20002; i++) {
             a=new TestAt();
             t=new Thread(a);
            t.start();
        }
        t.join();
        System.out.println(a.getCount());
    }
}
